﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace ToDoClear
{
    public class ToDoItem : Canvas
    {
        static byte r = 5;
        //内部ui,用于分裂时隐藏的

        List<UIElement> collection;
        Button newBtn;
        string strContent;
        public ToDoItem(string text)
        {
            
            r+=10;
            this.Background=new SolidColorBrush(Color.FromArgb(255,r,34,23));
            this.Width = ConstVar.ItemW;
            this.Height = ConstVar.ItemH;
            newBtn = new Button();
            newBtn.Content = text;
            this.Children.Add(newBtn);
            strContent = text;
            collection = new List<UIElement>();
            collection.Add(newBtn);

            //变量初始化
            currentAngle = 0;
            currentH = ConstVar.ItemH;
        }

        public double MeasureHeight()
        {
            return currentH;
        }

        //得到一个和自己一样的元素
        private ToDoItem Copy()
        {
            ToDoItem newItem = new ToDoItem(strContent);
            return newItem;
        }

        #region  旋转相关

        public double currentH;
        public double currentAngle = 0;

     
        //返回的是角度，是度数 0-180度
        public double cos2angle(double cosValue)
        {
            return Math.Acos(cosValue) / Math.PI * 180;
        }

        //如果是上面旋转,下面保持，offset会为一个负值 
        public double getDyOffset()
        {
            if (currentAngle < 0)
            {
                return currentH - ConstVar.ItemH;
            }
            return 0;
        }

        //下面保持不动，上面旋转，-x度
        public void FlipTop(double desireH)
        {
            if (desireH < 0)
                desireH = 0;
            PlaneProjection pp = new PlaneProjection();
            pp.CenterOfRotationX = 0;
            pp.CenterOfRotationY = 1;
            pp.CenterOfRotationZ = 0;
            
            if (desireH >= ConstVar.ItemH)
            {
                desireH = ConstVar.ItemH;
                this.Projection = null;
                currentAngle = 0;
                currentH = desireH;
                return;
            }
            double cosValue = desireH / ConstVar.ItemH;

            currentAngle = -1 * cos2angle(cosValue);
            currentH = desireH;

            pp.RotationX = currentAngle;

            this.Projection = pp;
        }

        //上面保持不动，下面旋转 x度
        public void FlipBottom(double desireH)
        {
            if (desireH < 0)
                desireH = 0;
            PlaneProjection pp = new PlaneProjection();
            pp.CenterOfRotationX = 0;
            pp.CenterOfRotationY = 0;
            pp.CenterOfRotationZ = 0;
            if (desireH >= ConstVar.ItemH)
            {
                desireH = ConstVar.ItemH;
                this.Projection = null;
                currentAngle = 0;
                currentH = desireH;
                return;
            }

            double cosValue = desireH / ConstVar.ItemH;

            currentAngle = cos2angle(cosValue);
            currentH = desireH;

            pp.RotationX = currentAngle;

            this.Projection = pp;
        }
        public void ResumeSize()
        {
            this.Projection = null;
            currentAngle = 0;
            currentH =ConstVar.ItemH;
        }
        #endregion


        #region 分裂相关


        public void Split(double desireH)
        {
            if (desireH < 0)
                desireH = 0;

            if (desireH >= ConstVar.ItemH)
            {
                desireH = ConstVar.ItemH;
                //设置当前高度
                currentH = desireH;
                //恢复分裂
                ResumeSplit();
            }
            else
            {
                //分裂
                DoSplit();

                //设置当前高度
                currentH = desireH;

                //翻转上面的，要固定上沿，翻转下半部分
                topItem.FlipBottom(desireH);
                RectangleGeometry topClip = new RectangleGeometry();
                //只截上面的一半
                topClip.Rect = new Rect(0, 0, ConstVar.ItemW, ConstVar.ItemH / 2);
                topItem.Clip = topClip;
                Canvas.SetTop(topItem, 0);
                Canvas.SetLeft(topItem, 0);

                //先翻转,要固定下沿，翻转上半部分
                bottomItem.FlipTop(desireH);
                RectangleGeometry bottomClip = new RectangleGeometry();
                //只截下面的一半
                bottomClip.Rect = new Rect(0, ConstVar.ItemH/2, ConstVar.ItemW, ConstVar.ItemH);
                bottomItem.Clip = bottomClip;

                Canvas.SetTop(bottomItem, bottomItem.getDyOffset());

            }

        }

        bool isSplit = false;
        ToDoItem topItem;
        ToDoItem bottomItem;
        //分裂啊
        private void DoSplit()
        {
            if (isSplit)
                return;
            topItem = this.Copy();
            bottomItem = this.Copy();
            HideContent();
            this.Children.Add(topItem);
            this.Children.Add(bottomItem);
            

            isSplit = true;
        }
        //分裂恢复
        private void ResumeSplit()
        {
            if (!isSplit)
                return;

            ResumeContent();
            this.Children.Remove(topItem);
            this.Children.Remove(bottomItem);
            topItem = null;
            bottomItem = null;

            isSplit = false;
        }
        //分裂时，隐藏原来的内容，加入分裂元素
        private void HideContent()
        {
            foreach (UIElement ui in collection)
            {
                ui.Visibility = Visibility.Collapsed;
            }
        }
        private void ResumeContent()
        {
            foreach (UIElement ui in collection)
            {
                ui.Visibility = Visibility.Visible;
            }
        }
        #endregion

    }
}
